package net.melon.review.inst.struts2;

import javax.annotation.Resource;

import net.esj.basic.utils.StringUtils;
import net.esj.basic.utils.view.BaseJsonMessage.MessageLevel;
import net.esj.basic.view.AbstractPluginPageAction;
import net.melon.review.inst.service.ReviewService;
import net.melon.review.pojo.Area;
import net.melon.review.pojo.Review;
import net.melon.review.pojo.Review.ReviewIntentionPrice;
import net.melon.review.pojo.Review.ReviewState;
import net.melon.sso.pojo.Member;
import net.melon.uploader.Uploader;

public class ReviewAction extends AbstractPluginPageAction<Review, Review>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5020737692158852612L;
	
	@Resource
	private ReviewService reviewService;
	
	@Override
	protected void afterQueryPage() {
		String path = Uploader.getDownUrl("review");
		for(Object object : pagination.getRows()){
			Review review = (Review)object;
			Review.review_path = path;
			Area province = reviewService.getAreaMapByKey(review.getProvince());
			Area city = reviewService.getAreaMapByKey(review.getCity());
			if(province!=null){
				review.setProvinceStr(province.getName());
			}
			if(city!=null){
				review.setCityStr(city.getName());
			}
			if(StringUtils.hasText(review.getAuthorization())){
				review.setAuthorization(review.getAuthorization());
			}
			if(StringUtils.hasText(review.getIdentitys())){
				for(String s : review.getIdentitys().split(",")){
					review.getIdentityList().add(s);
				}
			}
			if(StringUtils.hasText(review.getAccountBook())){
				for(String s : review.getAccountBook().split(",")){
					review.getAccountBookList().add(s);
				}
			}
			
			if(StringUtils.hasText(review.getMarriageCertificate())){
				for(String s : review.getMarriageCertificate().split(",")){
					review.getMarriageCertificateList().add(s);
				}
			}
			if(StringUtils.hasText(review.getHouseCertificate())){
				for(String s : review.getHouseCertificate().split(",")){
					review.getHouseCertificateList().add(s);
				}
			}
			if(StringUtils.hasText(review.getIncome())){
				for(String s : review.getIncome().split(",")){
					review.getIncomeList().add(s);
				}
			}
			if(StringUtils.hasText(review.getBank())){
				for(String s : review.getBank().split(",")){
					review.getBankList().add(s);
				}
			}
			if(StringUtils.hasText(review.getDriversLicense())){
				for(String s : review.getDriversLicense().split(",")){
					review.getDriversLicenseList().add(s);
				}
			}
			if(StringUtils.hasText(review.getMemberId())){
				Member m = defaultService.get(review.getMemberId(), Member.class);
				if(m!=null){
					review.setMemberName(m.getMobile());	
				}
			}
			
		}
	}
	
	public void noPass(){
		try {
			reviewService.changeState(Integer.valueOf(id),ReviewState.NO_PASS.getState());
			writeBaseJsonMessage(getText(SYSTEM_MESSAGE_TITLE,""), addSuccMsg(), MessageLevel.INFO, "html");
		} catch (Exception e) {
			writeBaseJsonMessage(getText(SYSTEM_MESSAGE_TITLE,""), e.getMessage(), MessageLevel.ERROR, "html");
		}
	}
	
	public void pass(){
		try {
			reviewService.changeState(Integer.valueOf(id),ReviewState.PASS.getState());
			writeBaseJsonMessage(getText(SYSTEM_MESSAGE_TITLE,""), addSuccMsg(), MessageLevel.INFO, "html");
		} catch (Exception e) {
			writeBaseJsonMessage(getText(SYSTEM_MESSAGE_TITLE,""), e.getMessage(), MessageLevel.ERROR, "html");
		}
		
	}
	
	public ReviewIntentionPrice[] getPrices(){
		return ReviewIntentionPrice.values();
	}
	
	public ReviewState[] getStates(){
		return ReviewState.values();
	}

}
